﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;

namespace CMS
{
    public partial class ctrProduct : System.Web.UI.UserControl
    {
        private string cateId = string.Empty;
        private int Page;
        private string ps = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(ctrProduct));
                try
                {
                    cateId = Request.QueryString["CateId"].ToString();
                }
                catch
                {
                    cateId = null;

                }
                lblHTML.Text += Common.Utilities.AttachJavascript("Script", "jscript.js");
                lblHTML.Text += StrProduct(cateId);
            }
        }

        public string StrProduct(string cateId)
        {
            try
            {
                Page = Convert.ToInt32(Request.QueryString["Page"].ToString());

            }
            catch
            {
                Page = 1;
            }

            string strHtml = string.Empty;
            string sql = string.Empty;
            string s = string.Empty;
            string strPhantrang = string.Empty;
            strHtml += "<div id=\"Div2\" class=\"flower\" style=\"border-radius: 10px; -webkit-border-radius: 10px;-moz-border-radius: 10px; background-image: url('images/bg_head_product.jpg');background-repeat: repeat; height: 40px;\">";
            strHtml += "<h3>Sản phẩm " + strCateName() + " </h3></div>";

            strHtml += "<div id=\"Div1\" class=\"post\">";
            if (Utilities.IsNullOrWhiteSpace(cateId))
            {
                sql = string.Format("exec spProduct_PhanTrang {0},{1},{2},'',null ", Page, 8, 3);
            }
            else
            {
                sql = string.Format("exec spProduct_PhanTrang {0},{1},{2},'{3}',{4} ", Page, 8, 3,"and CategoryId="+cateId, cateId);

            }
            var dsProduct = DAL.GetDataSet(sql);

            if (dsProduct != null && dsProduct.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsProduct.Tables[0].Rows)
                {
                    if (Convert.ToBoolean(dr["isPromotion"]) != true)
                    {
                        dr["Promotion"] = string.Empty;
                    }

                    if (string.IsNullOrEmpty(dr["Image"].ToString()))
                    {
                        dr["image"] = Common.Utilities.ImagesUrl() + "Nam-vang.PNG";
                    }
                    strHtml += "<div class=\"ClassPage\">";
                    strHtml += "  <table width=\"100%\" cellpadding=\"1\" cellspacing=\"1\"><tr>";
                    strHtml += "<td  align=\"left\" valign=\"top\">";
                    strHtml += "<table id=\"Table1\">";
                    strHtml += "<tbody>";
                    strHtml += "<tr>";
                    strHtml += "<td colspan=\"2\" class=\"tdProductName\" height=\"30\">";
                    strHtml += "<a href= Default.aspx?Cateid=" + dr["CategoryId"] + "&ProductId=" + dr["ProductId"] + ">" + dr["ProductName"] + "</a>";
                    strHtml += "            </td></tr><tr>";
                    strHtml += "            <td valign=\"top\" width=\"120px\" height=\"120px\">";
                    strHtml += "            <a href=Default.aspx?Cateid=" + dr["CategoryId"] + "&ProductId=" + dr["ProductId"] + "><img src=" + dr["Image"] + " border=\"0\"></a>";
                    strHtml += "            </td><td valign=\"top\" height=\"120px\">" + dr["ShortTitle"] + "</td>";
                    strHtml += "            </tr><tr><td class=\"Descriptionproduct\">Giá bán:" + dr["RealPrice"] + "VNĐ</td>";
                    strHtml += "            <td><a href= \"javascript:ClientAdd2ShoppingCart(" + dr["ProductId"] + ")\"><img src=\"../images/cart.gif\" border=\"0\" />";
                    strHtml += "            </a></td></tr><tr><td colspan=\"2\">";
                    strHtml += "            <i style=\"color: rgb(255, 0, 0); font-family: verdana; font-size: 11px; font-variant: normal;font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto;text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);\">";
                    strHtml += "            " + dr["Promotion"] + "</i>";
                    strHtml += "</td></tr></tbody></table></td></tr></table>";
                    strHtml += "</div>";
                }
            }
            else
            {
                strHtml += "Hiện mặt hàng này chưa có hoặc đã bị hết hàng. Vui lòng liên hệ với chúng tôi";
            }
            strHtml += "</div>";

            if (dsProduct != null && dsProduct.Tables[1].Rows.Count > 0)
            {
                strPhantrang += "<div id=\"Div21\" class=\"BottomPanelPagin\" >";
                strPhantrang += "<h3 class=\"h3\">" + dsProduct.Tables[1].Rows[0]["Phantrang"] + "</h3></div>";
            }

            return strHtml + strPhantrang;
        }

        public string strCateName()
        {
            string sql = string.Format("exec tblCategory_GetAllByCondition '{0}' ", "AND CategoryId=" + cateId);
            var dsCate = DAL.GetDataSet(sql);
            if (dsCate != null && dsCate.Tables[0].Rows.Count > 0)
            {
                DataRow dr = dsCate.Tables[0].Rows[0];
                ps = ">>" + "<a style=\"text-decoration:none;color: #FFFFFF;\" href= Default.aspx?Cateid=" + dr["CategoryId"] + ">" + dr["CategoryName"] + "</a>";
            }
            return ps;
        }

        [AjaxPro.AjaxMethod]
        public static string ServerSideAddShoppingCart(int ProductId)
        {
            try
            {
                ShopCart.Add2Cart(ProductId);
                return Common.ResourceBLL.GetFrontEndText("Vi", "ctrProduct", "sp", "Sản phẩm bạn đặt đã đưa vào giỏ hàng!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}